package com.pg_chat.controller;

import com.alibaba.fastjson.JSONObject;
import com.pg_chat.config.MsgConfig;
import com.pg_chat.model.UserEntity;
import com.pg_chat.service.ChatService;

import com.pg_chat.utils.JsonUtil;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.slf4j.Logger;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.util.Date;

@RestController("chatController")
@RequestMapping("/chat")
public class ChatController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource(name = "chatService")
    private ChatService chatService;

    //历史记录
    @RequestMapping("/history")
    public String showHistoryMessage(HttpServletRequest request)throws Exception{
        MsgConfig<JSONObject> msgConfig = new MsgConfig();

        HttpSession session = request.getSession();
        String userId = (String) session.getAttribute("user");
        //获取好友Id
        String firId = request.getParameter("friendsId");
        //获取时间
        Date dateBegin = DateFormat.getInstance()
                .parse(request.getParameter("dateBegin"));
        Date dateEnd = DateFormat.getInstance()
                .parse(request.getParameter("dateEnd"));

        JSONObject historyMessage;
        JSONObject json;
        try{
            historyMessage = chatService
                    .historyMessage(userId, firId, dateBegin, dateEnd);
        }catch (Exception e){
            json = JsonUtil.resultMsg(msgConfig.fMsg("获取记录"));
            e.printStackTrace();
            return json.toJSONString();
        }

        json = JsonUtil.resultMsg(msgConfig.sucMsg(historyMessage,
                "获取成功"));
        return json.toJSONString();

    }

    @RequestMapping("/wordCloud")
    public String wordCloud(HttpServletRequest request){

        JSONObject json = new JSONObject();
        return json.toJSONString();
    }

    @RequestMapping("/chatRoom")
    public ModelAndView chatRoom(HttpServletRequest request){
        HttpSession session = request.getSession();
        UserEntity user = (UserEntity) session.getAttribute("user");
        Integer friendId =  Integer.parseInt(request.getParameter("friendId"));

        ModelAndView page = new ModelAndView("/pages/talkRoom/client");
        page.addObject("userMessage",session.getAttribute(""));

        return page;
    }
}
